function Pickup(pl,key)
	if (key == IN_USE) then
		if (pl.Charge == nil ) then
			pl.Charge = os.time()
		end
	end
end

function KeyReleased(pl, key)
	if (key == IN_USE) then
		if (!pl.Holding) then
			local tr = util.QuickTrace(pl:GetShootPos(),pl:GetAimVector()*100,pl)
			if ( ValidEntity(tr.Entity) and tr.Entity:GetClass() == "prop_physics" and tr.Entity:GetPhysicsObject():IsMoveable() ) then
				pl.Holding = tr.Entity
				pl.Charge = nil
			end
		else
			pl.Charge = os.time() - pl.Charge
			if pl.Charge > 3 then
				pl.Charge = 3
			end
			pl.Holding:GetPhysicsObject():SetVelocity(pl:GetAimVector()*7000/(3*pl.Holding:GetPhysicsObject():GetMass())*pl.Charge)
			pl.Charge = nil
			pl.Holding = nil
			pl.WasHolding = true
		end
	end
end

function Think_Pickup()
	for k,v in pairs(player.GetAll()) do
		if (v.Holding) then
			if (v.Holding:IsValid()) then
				local Target = v:GetShootPos() + v:GetAimVector() * 60;
				local Start = v.Holding:GetPos();
				local Vel = Target - Start;
				if Vel.x > 200 then
					Vel.x = 200
				elseif Vel.y > 200 then
					Vel.y = 200
				elseif Vel.z > 200 then
					Vel.z = 200
				elseif Vel.x < -200 then
					Vel.x = -200
				elseif Vel.y < -200 then
					Vel.y = -200
				elseif Vel.z < -200 then
					Vel.z = -200
				end
				v.Holding:GetPhysicsObject():SetVelocity(Vel*30)
				if v.Holding:GetPos():Distance(v:GetPos()) > 150 then
					v.Holding = nil
					v.WasHolding = false
				elseif (!v:Visible(v.Holding)) then
					v.Holding = nil
					v.WasHolding = false
				elseif v:GetPos().z > v.Holding:GetPos().z then
					v.Holding = nil
					v.WasHolding = false
				elseif !(v:Alive()) then
					v.Holding = nil
					v.WasHolding = false
				end
			else
				v.Holding = nil
				v.WasHolding = false
			end
		end
	end
end

function ButtonUse( activator, entity )
	if activator.WasHolding then
		activator.WasHolding = false
		return false
	end
end
 
hook.Add( "PlayerUse", "ButtonUse", ButtonUse ) 
hook.Add( "KeyPress", "KeyPressedHook", KeyPressed ) 
hook.Add( "KeyRelease", "KeyPressedHook", KeyReleased ) 
hook.Add("Think","Think_Pickup",Think_Pickup)
hook.Add("KeyPress","Pickup",Pickup)